/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */

package TUILayer;
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.AddressCtr;
import ModelLayer.*; 

public class AddressBookUI
{   

    private AddressCtr addrCtr;
    
    /**
     * Constructor for objects of class AddressBookUI
     */
    public AddressBookUI()
    {
        addrCtr = new AddressCtr();
    }
    
    /**
     * Menu Section
     * 
     * start(), executes the menu.
     * addressMenu(), is a selection method for the user menu.
     * writeAddressBookMenu(), is a visual interpretation of the user menu for the addressbook section.
     */
    public void start()
    {
        addressMenu();
    }
    
    public void addressMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeAddressBookMenu(); 
            if(choise == 1)
            {   
                startGetPerson();
            }
            else
            {
                if(choise == 2)
                {
                    startAddPerson();
                }
                else
                {
                    if(choise == 3)
                    {
                        startDeletePerson();
                    }
                    else
                    {
                        if(choise == 4)
                        {
                            startEditPerson();
                        }
                        else
                        {
                            if(choise == 5)
                            {
                                startListAllPersons();
                            }
                            else
                            {
                                exit = true;
                            }
                        }
                    }
                }
            }
        }
    }
    
    public int writeAddressBookMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** AddressBook Menu ***");
        System.out.println(" (1) Show information about person");
        System.out.println(" (2) Add person");
        System.out.println(" (3) Delete person");
        System.out.println(" (4) Update person");
        System.out.println(" (5) List all persons");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");
        
        int choise = keyboard.nextInt();
        return choise;
    }
    
    /**
     * User input section
     * 
     * inputName(), inputs the persons name via user interaction.
     * inputPostalCode(), inputs the persons postal code via user interaction.
     * inputCity(), inputs the persons city via user interaction.
     * inputPhone(), inputs the persons phone via user interaction.
     * inputAddress(), inputs the persons address via user interaction.
     */
    public String inputName()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    public String inputPostalCode()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    public String inputCity()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    public String inputPhone()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
     
    public String inputAddress()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    /**
     * User interaction section
     * 
     * startAddPerson(), creates a persons in addressbook and stores it in array.
     * startGetPerson(), finds the persons information trough there phonenumber via its perspectiv array.
     * startDeletePerson(), deletes persons if there phonenumber is found in array.
     * startEditPerson(), edits the persons information after there phonenumber is found via there perspectiv arrays.
     * editPersonData(), is a visual interpretation of the person edit menu.
     * startListAllPersons(), lists all persons in array.
     */
    private void startAddPerson()
    {
        String name = inputName();
        String address = inputAddress();
        String postalCode = inputPostalCode();
        String city = inputCity();
        String phone = inputPhone();
        addrCtr.createPerson(name, address, postalCode, city, phone);
        
    }
   
    private void startGetPerson()
    {
        String phone = inputPhone();
        
        Person person = null;
        person = addrCtr.getPerson(phone);
        
        try 
        {
            if (person != null)
            {
                System.out.println(" -- Person Information --");
                System.out.println(person.getName());
                System.out.println(person.getAddress());
                System.out.println(person.getCity() + ", " + person.getPostalCode());
                System.out.println("\n Press the enter key to return to menu");
                Scanner sc = new Scanner(System.in);
                while(!sc.nextLine().equals(""));
            }
            else
            {
                System.out.println("\n ** No person with that phone number was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
        
    }
    
    private void startDeletePerson() 
    {
        String phone = inputPhone();
        
        try
        {
            if(addrCtr.deletePerson(phone))
            {
                System.out.println("\n **Person deleted! **");
                Thread.sleep(1500);
            }
            else
            {
                System.out.println("\n ** No person with that phone number was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
    }
    
    private void startEditPerson() 
    {
        String phone = inputPhone();
        
        Person person = null;
        person = addrCtr.getPerson(phone);
        
        try
        {
            if(person != null)
            {
                int nr = editPersonData();
                Scanner keyboardData = new Scanner(System.in); 
                System.out.print(" Enter new data: ");
                String newData = keyboardData.nextLine();
                addrCtr.editPersonData(person, nr, newData);
            }
            else
            {
                System.out.println("\n ** No person with that phone number was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
    }
    
    private int editPersonData() 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" (1) Edit Name");
        System.out.println(" (2) Edit Address");
        System.out.println(" (3) Edit Postal code");
        System.out.println(" (4) Edit City");
        System.out.println(" (5) Edit Phone");
        
        int choise = keyboard.nextInt();
        return choise;
    }
    
    private void startListAllPersons() 
    {
        ArrayList<Person> addrList = addrCtr.listAllPersons();
        
        for (Person person : addrList)
        {
            System.out.println(" -- Person Information --");
            System.out.println(person.getName());
            System.out.println(person.getAddress());
            System.out.println(person.getCity() + ", " + person.getPostalCode());
            System.out.println("Phone: " + person.getPhone());
            System.out.println("");
        }
        
        Scanner sc = new Scanner(System.in);
        while(!sc.nextLine().equals(""));
    }
}